﻿{{~ dto = entity_type + "Dto" ~}}
{{~ query_dto = entity_type + "QueryDto" ~}}
{{~ for using_item in usings ~}}
using {{ using_item }};
{{~ end ~}}

namespace {{ namespace }}
{
    public class {{ class_name }} : AppServices<{{entity_type}}>, {{ interface_name }}
    {

        public {{ class_name }}(ISqlSugarClient db) : base(db)
        {

        }

        public {{ return_type }} Create({{ entity_type }} entity)
        {
            // TODO: 添加创建逻辑
            return Context.Insertable(entity).ExecuteReturnEntity();
        }

        public {{ return_type }} GetById({{ primary_key.data_type }} id)
        {
            // TODO: 添加获取逻辑
            return Context.Queryable<{{ entity_type }}>().InSingle(id);
        }
        
        public PageResponse<{{ return_type }}> GetPageList({{ query_dto }} query)
        {
            var queryable = Context.Queryable<{{ entity_type }}>();
            var exp = Expressionable.Create<{{ entity_type }}>();
            exp = exp
{{~ for column in query_columns ~}}
    {{~ if column.data_type == "string" ~}}
                  .AndIF(!string.IsNullOrEmpty(query.{{ column.field_name }}), s => s.{{ column.field_name }}.Contains(query.{{ column.field_name }}))
    {{~ else if column.data_type == "int" ~}}
                  .AndIF(query.{{ column.field_name }} != default, s => s.{{ column.field_name }} == query.{{ column.field_name }})
    {{~ else if column.data_type == "DateTime"~}}
                  .AndIF(query.Begin{{ column.field_name }}.HasValue,s => s.{{ column.field_name }} >= query.Begin{{ column.field_name }})
                  .AndIF(query.End{{ column.field_name }}.HasValue,s => s.{{ column.field_name }} <= query.End{{ column.field_name }})
    {{~ end ~}}
{{~ end ~}};
            var page = queryable
                 .Where(exp.ToExpression())
                 .ToPageList<{{ entity_type }},{{ return_type }}>(query);
             return page;
        }

        public bool Update({{ entity_type }} entity)
        {
            // TODO: 添加更新逻辑
            return Context.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params {{ primary_key.data_type }}[] {{ primary_key.field_name }})
        {
            // TODO: 添加删除逻辑
            return Context.Deleteable<{{ entity_type }}>({{ primary_key.field_name }}).ExecuteCommandHasChange();
        }
    }
}
